#ifndef SNAKE_H
#define SNAKE_H

#include "Container.h"
#include <vector>
#include <deque>
#include <atomic>
#include <thread>
#include <mutex>

const int CHARACTER_COUNT = 3; // 角色数量

enum ClotherType
{
    CLOTHING_DEFAULT,    // 默认头像
    CLOTHING_FLOWER_HAT, // 花帽
    CLOTHING_SUNGLASSES  // 太阳镜
};

extern char *characterImages[];

class Snake
{
private:
    std::vector<Container *> bodySegments;           // 蛇身段容器
    std::deque<std::pair<int, int>> positionHistory; // 位置历史记录
    int segmentSize;                                 // 每个蛇身段的大小
    int overlapAmount;                               // 重叠量（像素）
    int maxHistoryLength;                            // 最大历史记录长度

    // 蛇的移动相关
    atomic<int> currentDirection; // 当前移动方向
    atomic<bool> isMoving;        // 是否在移动
    atomic<bool> isRunning;       // 线程是否在运行
    thread moveThread;            // 移动线程
    int moveSpeed;                // 移动速度（毫秒）

    void moveThreadFunc(); // 移动线程函数

    int Status = Normal;

    char *Snake_head = "./res/head.bmp";
    char *Snake_body = "./res/body.bmp";
    char *Snake_tail = "./res/tail.bmp";

    enum status
    {
        Normal,
        Cold,
        Hot
    };

public:
    Snake(Container *parent, int segmentSize = 30, int overlap = 15, int initialLength = 3, int speed = 200, ClotherType type = CLOTHING_DEFAULT);
    ~Snake();

    void moveHead(int newX, int newY); // 移动蛇头
    void updateBody();                 // 更新蛇身位置
    void grow();                       // 增加蛇身长度
    void shrink(int amount = 1);       // 减少蛇身长度
    Container *getHead() const;        // 获取蛇头容器

    char *get_Snake_head() { return Snake_head; }
    char *get_Snake_body() { return Snake_body; }
    char *get_Snake_tail() { return Snake_tail; }

    void change_head() { Snake_head = "./res/head.bmp"; }
    void change_body() { Snake_body = "./res/body.bmp"; }
    void change_tail() { Snake_tail = "./res/tail.bmp"; }

    void change_head_hot() { Snake_head = "./res/head_hot.bmp"; }
    void change_body_hot() { Snake_body = "./res/body_hot.bmp"; }
    void change_tail_hot() { Snake_tail = "./res/tail_hot.bmp"; }

    void change_head_cold() { Snake_head = "./res/head_cold.bmp"; }
    void change_body_cold() { Snake_body = "./res/body_cold.bmp"; }
    void change_tail_cold() { Snake_tail = "./res/tail_cold.bmp"; }

    // 方向控制
    void setDirection(int direction);
    int getDirection() const { return currentDirection.load(); }
    void startMoving();
    void stopMoving();
    void changemoveSpeed(int speed) { moveSpeed = speed; }
    void changeStatus(int status) { Status = status; };

    void setBodySegmentsImage(const char *imagePath); // 修改所有身体节点的背景图片
    void setTailImage(const char *imagePath);         // 修改蛇尾的背景图片
    void setHeadImage(const char *imagePath);         // 修改蛇头的背景图片

    // 碰撞检测
    bool checkBoundaryCollision(int maxWidth, int maxHeight) const;
    bool checkCollisionWithContainer(Container *other) const;

    // 获取蛇身信息
    bool isMove() { return isMoving; }
    int getLength() const { return bodySegments.size(); }
    int getSegmentSize() const { return segmentSize; }
    int getOverlapAmount() const { return overlapAmount; }
};

// 方向枚举
enum Direction
{
    DIRECTION_NONE = 0,
    DIRECTION_RIGHT = 1,     // 右
    DIRECTION_UP_RIGHT = 2,  // 右上
    DIRECTION_UP = 3,        // 上
    DIRECTION_UP_LEFT = 4,   // 左上
    DIRECTION_LEFT = 5,      // 左
    DIRECTION_DOWN_LEFT = 6, // 左下
    DIRECTION_DOWN = 7,      // 下
    DIRECTION_DOWN_RIGHT = 8 // 右下
};

#endif